//
//  VoucherListHeaderView.m
//  JiaoX
//
//  Created by Northwan on 15/4/24.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "VoucherListHeaderView.h"

@interface VoucherListHeaderView()
@property(nonatomic,strong)UILabel *headerTipView;
@property(nonatomic,strong)UIView *lineView;
@end
@implementation VoucherListHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //查询按钮
        _voucherSearch = [[UIButton alloc]init];
        [_voucherSearch.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
        [_voucherSearch setContentMode:UIViewContentModeCenter];
        [_voucherSearch setFrame:CGRectMake(JXScreenW-70-JXPublicInset, 15, 70, 40)];
        [_voucherSearch setTitle:@"验证" forState:UIControlStateNormal];
        [_voucherSearch setTintColor:[UIColor whiteColor]];
        [_voucherSearch setBackgroundImage:[UIImage resizedImage:@"btn_login_bg"] forState:UIControlStateNormal];
        [_voucherSearch addTarget:self action:@selector(didSearch) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_voucherSearch];
        //输入框
        _voucherNumber = [[UITextField alloc]init];
        [self initTextField:_voucherNumber frame:CGRectMake(JXPublicInset, 15, JXScreenW-JXPublicInset*3-_voucherSearch.width , 40) placeholderText:@"输入您的现金序列号"];
        
        //灰色的条
        _lineView = [[UIView alloc]init];
        [_lineView setFrame:CGRectMake(0, CGRectGetMaxY(_voucherSearch.frame) + 15, JXScreenW, 10)];
        [_lineView setBackgroundColor:JXColor(238, 238, 238)];
        [self addSubview:_lineView];
        //头部提示
         _headerTipView = [[UILabel alloc]init];
        [_headerTipView setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:13]]];
        [_headerTipView setTextColor:JXColor(51, 51, 51)];
        [_headerTipView setFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_lineView.frame)+JXPublicInset, JXScreenW - JXPublicInset*2, 30)];
        [_headerTipView setContentMode:UIViewContentModeCenter];
        [self addSubview:_headerTipView];
        [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(_headerTipView.frame))];
    }
    return self;
}

// 初始化输入框
-(void)initTextField :(UITextField *)edit frame:(CGRect)frame placeholderText:(NSString *)placeholderText{
    //设置边框
    [edit.layer setBorderColor:JXColor(204, 204, 204).CGColor];
    [edit.layer setBorderWidth:0.7];
    [edit.layer setCornerRadius:5];
    
    edit.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    edit.attributedPlaceholder =[[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: JXColor(204, 204, 204)}];
    edit.frame = frame;
    edit.clearButtonMode = UITextFieldViewModeAlways;
    [self textFieldAddleftView:edit];
    [self addSubview:edit];
}

//输入框添加左边距
-(void)textFieldAddleftView:(UITextField *)edit{
    CGRect frame = [edit frame];
    frame.size.width = 7.0f;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    edit.leftViewMode = UITextFieldViewModeAlways;
    edit.leftView = leftview;
}

-(void)didSearch{
    NSString *text =  _voucherNumber.text;
    //发送点击的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"voucher_search_did" object:text];
}

-(void)setTipText:(NSString *)text{//设置提示文字
    [_headerTipView setText:text];
}

-(void)hideTipView{//隐藏提示的View
    [_headerTipView setHidden:YES];
    [self setFrame:CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(_lineView.frame))];
}

@end
